package org.obsidianrad.utils;

import java.lang.reflect.Array;

/**
 * 
 * @author eliosh
 * @version $Rev: 4 $
 * @lastautorcommit $Author: eliosh $
 * @lastdatecommit $Date: 2009-10-31 12:37:28 +0100(sab, 31 ott 2009) $
 */
public class Utils {
	/**
	 * Reallocates an array with a new size, and copies the contents of the old
	 * array to the new array.
	 * 
	 * @param oldArray
	 *            the old array, to be reallocated.
	 * @param newSize
	 *            the new array size.
	 * @return A new array with the same contents.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T[] resizeArray(T[] oldArray, int newSize){
		int oldSize = oldArray.length;
		T[] newArray = (T[]) Array.newInstance(oldArray.getClass().getComponentType() , newSize);
		int preserveLength = Math.min(oldSize, newSize);
		if (preserveLength > 0){
			System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
		}
		return newArray;
	}

	
}
